<?php

$oTpl = &App::getTemplate();

$oReq = &App::getRequest();

$oDb = &Database::get();

$oLang = &App::getLanguage();

App::loadClass('Validator');

$iId = $oReq->get('id');

if ( $oReq->get('save') )
{
	$aPost = $oReq->getArray('val');

	$sVarName = ($aPost['module'] ? $aPost['module'] . "." : '' ) . str_replace(" ", "_", strtolower($aPost['varname']));

	$aItem = $oDb->getRow("SELECT varname, text FROM ". App::getT('language_phrase') ."
		WHERE `varname` = '". $oDb->escape($sVarName) ."'" . ($iId ? " AND `languageid` = '". $iId ."'" : "" ));

	if ( isset($aItem['varname']) )
	{
		$sError = App::format('Mod_Language.already_added', $aItem['varname']);
	}
	else
	{
		if (is_array($aPost['text']))
		{
			foreach($aPost['text'] as $iLangId => $sLangValue)
			{
				$aQuery = array
				(
					'languageid' => $iLangId,
					'varname' => $sVarName,
					'text' => $sLangValue,
					'product' => 'konsort',
					'time' => time(),
					'version' => App::getParam('info.version'),
					'module' => $aPost['module'],
					'default' => $sLangValue,
				);
				$oDb->insert(App::getT('language_phrase'), $aQuery);
			}
		}
		else 
		{
			$aQuery = array
			(
				'languageid' => $iId,
				'varname' => $sVarName,
				'text' => $aPost['text'],
				'product' => 'konsort',
				'time' => time(),
				'version' => App::getParam('info.version'),
				'module' => $aPost['module'],
				'default' => $aPost['text'],
			);
			$oDb->insert(App::getT('language_phrase'), $aQuery);
		}

		$sTempFormat = App::format('Mod_Language.new_phrase_added', array($sVarName, $sVarName));

		$oLang->removeCachedFiles();

		App::gotoUrl('admin.language.add-phrase', array('id' => $iId, 'translate' => $oReq->get('translate')), $sTempFormat, false);
	}
}

$aFields = array
(
	'val[varname]'   => array('title' => App::format('Mod_Language.varname'), 'def' => 'required')
);
$oValidator = &new Validator($aFields, array());

$aModules = App::getParam('modules');

sort($aModules);

$oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
$oTpl->assignSrc('sValJs', $oValidator->makeJS());
$oTpl->assign(array
(
	'aModules' => $aModules,
	'aItem' => (isset($aPost) ? $aPost : ''),
	'sError' => (isset($sError) ? $sError : ''),
	'aLanguages' => $oLang->getLanguages(),
	'sTranslate' => ($oReq->get('translate') ? str_replace("'", '-', base64_decode($oReq->get('translate'))) : '')
));

?>